//
//  CXDestinationViewController.m
//  Travel
//
//  Created by dllo on 15/11/28.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "CXDestinationViewController.h"
#import "CXNetHandle.h"
#import "CXDestinationModel.h"
#import "CXLoadMBProgress.h"
#import "CXCollectionViewHeadReusableView.h"
#import "CXDestinationCollectionViewCell.h"
#import "CXDestinationDetailViewController.h"
#import "CXDestinationMoreViewController.h"
#import "CXDestinationDataModel.h"
#import "CXDestinationCityListViewController.h"
#import "CXErrorViewController.h"

@interface CXDestinationViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

//外层数据
@property (nonatomic, retain) NSArray *dataArr;
@property (nonatomic, retain) UICollectionView *collectionView;
//每个分区数组
@property (nonatomic, retain) NSArray *arr_data_model;

@property (nonatomic, strong) id result_nil;

@end

@implementation CXDestinationViewController

- (void)dealloc
{
    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    
    [self prepareData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"目的地";
    
    [self prepareData];
    [self createCollectionView];
    
}

- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((WIDTH - 30) / 2, (WIDTH - 30) / 2);
    layout.headerReferenceSize = CGSizeMake(WIDTH, 30);
    layout.sectionInset = UIEdgeInsetsMake(15, 10, 20, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.bounces = NO;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[CXDestinationCollectionViewCell class]forCellWithReuseIdentifier:@"collection_item"];
    [self.collectionView registerClass:[CXCollectionViewHeadReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collection_head"];
    
    [CXLoadMBProgress loadMBProgress:self.view];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.dataArr.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    CXCollectionViewHeadReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"collection_head" forIndexPath:indexPath];

    void(^INDEX)(NSString *) = ^(NSString *str){
        CXDestinationMoreViewController *moreVC = [[CXDestinationMoreViewController alloc] init];
        moreVC.index = str;
        [self.navigationController pushViewController:moreVC animated:YES];
    };
    
    headerView.Index = INDEX;
    headerView.model_destination = self.dataArr[indexPath.section];
    
    return headerView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[self.dataArr[section] arr_data] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CXDestinationCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection_item" forIndexPath:indexPath];
    self.arr_data_model = [NSArray array];
    self.arr_data_model = [CXDestinationDataModel transformWithArray:[self.dataArr[indexPath.section] arr_data]];
    item.model_destination = self.arr_data_model[indexPath.item];
    return item;
}

#pragma mark - 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.arr_data_model = [NSArray array];
    self.arr_data_model = [CXDestinationDataModel transformWithArray:[self.dataArr[indexPath.section] arr_data]];
    
    NSString *str_valueId = [self.arr_data_model[indexPath.row] valueId] ;
    NSString *str_valueId_new = [str_valueId substringToIndex:1];
    NSInteger tag_id = [str_valueId_new integerValue];
    

    if ((tag_id > 0 && tag_id <= 9 ) || [str_valueId isEqualToString:@"HK"] || [str_valueId isEqualToString:@"MO"]) {
        CXDestinationDetailViewController *destinationDetailVC = [[CXDestinationDetailViewController alloc]init];
        destinationDetailVC.model_data = self.arr_data_model[indexPath.item];
        destinationDetailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:destinationDetailVC animated:YES];
    }else{
        CXDestinationCityListViewController *cityListVC = [[CXDestinationCityListViewController alloc] init];
        cityListVC.model_data = self.arr_data_model[indexPath.item];
        [self.navigationController pushViewController:cityListVC animated:YES];
    }

}

- (void)prepareData
{
    void (^ERROR)(id ) = ^(id result_again){
        self.result_nil = result_again;
    };
    
    if (self.result_nil == nil) {
        NSString *str_url = @"http://api.breadtrip.com/destination/v3/";
        [CXNetHandle getUrl:str_url cookie:nil result:^(id result) {
            
            if (result == nil) {
                CXErrorViewController *errorVC = [[CXErrorViewController alloc] init];
                
                errorVC.str_url = str_url;
                errorVC.Error = ERROR;
                [self presentViewController:errorVC animated:YES completion:nil];
                
            }else{
                self.result_nil = result;
                [CXLoadMBProgress removeMBProgress:self.view];
                
                NSArray *arr_elements_tmp = [NSArray array];
                 arr_elements_tmp = self.result_nil[@"elements"];
                self.dataArr = [CXDestinationModel transformWithArray:arr_elements_tmp];
                [self.collectionView reloadData];
            }
        }];
        
    } else {
        
        NSArray *arr_elements_tmp = [NSArray array];
        arr_elements_tmp = self.result_nil[@"elements"];
        self.dataArr = [CXDestinationModel transformWithArray:arr_elements_tmp];
        [self.collectionView reloadData];
    }
    
    
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
